﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using B10.Models;

namespace B10.Helpers
{
    public class ProfileService : B10.Helpers.IProfileService
    {
        IDatabaseRepository _db;

        public ProfileService(IDatabaseRepository db)
        {
            _db = db;
        }

        public ProfileService()
            : this(new DatabaseRepository())
        {

        }

        public UserProfile GetProfile(string username)
        {
            UserProfile p = _db.GetProfile(username);
            if (p == null)
                CreateNewProfile(username);
            return _db.GetProfile(username);
        }

        public int CreateNewProfile(string userName)
        {
            UserProfile profile = new UserProfile();
            profile.UserName = userName;
            profile.Avatar = "/Content/images/avatar.png";
            _db.Add<UserProfile>(profile);
            _db.Save();
            return profile.Id;
        }

        public int CreateFacebookProfile(Facebook.JSONObject user)
        {
            string name = GetFacebookUserName(user);
            UserProfile profile = _db.GetFBProfile(Facebook.FacebookConnect.UserID);
            if (profile == null)
            {
                profile = new UserProfile();
                profile.PrivateEmail = HttpUtility.HtmlEncode(GetFacebookProfileValue(user, "email"));
                profile.RealName = name;
                profile.Website = HttpUtility.HtmlEncode(GetFacebookProfileValue(user, "link"));
                profile.UserName = name;
                profile.Description = HttpUtility.HtmlEncode(GetFacebookProfileValue(user, "about"));
                profile.Avatar = HttpUtility.HtmlEncode(GetFacebookProfileValue(user, "picture"));
                if (string.IsNullOrEmpty(profile.Avatar))
                {
                    profile.Avatar = "/Content/images/avatar.png";
                }
                SetBirthDate(profile, user);
                SetGender(profile, user);
                _db.Add<UserProfile>(profile);
                _db.Save();
            }
            return profile.Id;
        }

        private void SetGender(UserProfile profile, Facebook.JSONObject user)
        {
            string gender = GetFacebookProfileValue(user, "gender");
            if (string.IsNullOrEmpty(gender))
                return;
            if (String.Equals(gender, Resources.Strings.Male,
                    StringComparison.InvariantCultureIgnoreCase))
                profile.Gender = GenderEnum.Male;
            else if (string.Equals(gender, Resources.Strings.Female,
                    StringComparison.InvariantCultureIgnoreCase))
                profile.Gender = GenderEnum.Female;
        }

        private void SetBirthDate(UserProfile profile, Facebook.JSONObject user)
        {
            string birth = GetFacebookProfileValue(user, "birthday");
            if (String.IsNullOrEmpty(birth))
                return;
            DateTime date;
            if (DateTime.TryParse(birth, out date))
                profile.BirthDate = date;
        }

        private string GetFacebookUserName(Facebook.JSONObject user)
        {
            string value = HttpUtility.HtmlEncode(GetFacebookProfileValue(user, "name"));
            if (!string.IsNullOrEmpty(value))
                return value;
            value = String.Format("{0} {1}",
                HttpUtility.HtmlEncode(GetFacebookProfileValue(user, "firstname")),
                HttpUtility.HtmlEncode(GetFacebookProfileValue(user, "lastname")));
            return value;
        }

        private string GetFacebookProfileValue(Facebook.JSONObject user, string name)
        {
            if (user == null)
                return null;
            if (!user.Dictionary.ContainsKey(name))
                return null;
            string s = user.Dictionary[name].String;
            if (String.IsNullOrEmpty(s))
                return null;
            return s.Trim();
        }

    }
}
